package com.cjw.controller.withMatching;

import com.cjw.config.annotation.OperationLogAnnotation;
import com.cjw.enums.ErrorCode;
import com.cjw.enums.systemManage.ExamineType;
import com.cjw.exceptions.BusinessException;
import com.cjw.exceptions.ContextException;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.livesquare.DetailPlanDTO;
import com.cjw.pojo.dto.livesquare.MyPlanDTO;
import com.cjw.pojo.dto.livesquare.PlanSimpleDTO;
import com.cjw.pojo.entity.contentReview.TextReponse;
import com.cjw.pojo.query.livesquare.AddPlanDTO;
import com.cjw.service.contentReview.TextCensor;
import com.cjw.service.livesquare.PlanService;
import com.cjw.service.systemManage.ExamineService;
import com.cjw.service.userMange.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author : cjw
 * @date: : 2022/2/12 12:23
 * @description : PlanController
 */
@RestController
@RequestMapping("/user/plan")
@Api("共同计划资源")
public class PlanController {
    @Autowired
    private PlanService planService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private UserAccountService accountService;

    @PostMapping("/findAllPlans")
    @ApiOperation("查看全部计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "查看全部计划",operDesc = "查询")
    public PageDTO<List<PlanSimpleDTO>> findAllPlans(Integer page, Integer size) {
        return planService.findAllPlans(page,size);
    }

    @PostMapping("/findListPlans")
    @ApiOperation("获取排行榜")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "查看全部计划",operDesc = "查询")
    public List<PlanSimpleDTO> findListPlans(Integer size) {
        return planService.findMaxListPlans(size);
    }

    @GetMapping("/findDetail")
    @ApiOperation("查看详细计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "查看详细计划",operDesc = "查询")
    public DetailPlanDTO findDetail(Long id) {
        return planService.findDetail(id);
    }

    @PostMapping("/joinPlan")
    @ApiOperation("参加计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "参加计划",operDesc = "添加")
    public Boolean joinPlan(Long id) {
        return planService.changePlan(id,0);
    }

    @PostMapping("/cancelPlan")
    @ApiOperation("取消计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "取消计划",operDesc = "修改")
    public Boolean cancelPlan(Long id) {
        return planService.changePlan(id,1);
    }

    @PostMapping("/clockInPlan")
    @ApiOperation("计划打卡")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "计划打卡",operDesc = "添加")
    public Boolean clockInPlan(Long id) {
        return planService.changeClock(id,0);
    }

    @PostMapping("/clockOutPlan")
    @ApiOperation("取消计划打卡")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "取消计划打卡",operDesc = "修改")
    public Boolean clockOutPlan(Long id) {
        return planService.changeClock(id,1);
    }

    @GetMapping("/getMyPlans/byTime")
    @ApiOperation("获取个人计划-通过时间")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "获取个人计划-通过时间",operDesc = "查询")
    public List<MyPlanDTO> getMyPlans(@RequestParam(value = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        return planService.getMyPlans(date);
    }

    @PostMapping("/addPlan")
    @ApiOperation("添加计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "添加计划",operDesc = "添加")
    public Boolean addPlan(@Valid @RequestBody AddPlanDTO addPlanDTO) {
        check(addPlanDTO.getContent());
        Long planId = planService.addPlan(addPlanDTO);
        examineService.addExamineByPerson(ExamineType.PLAN.getCode(), planId, addPlanDTO.getContent());
        return planId != null;
    }

    @GetMapping("/matchPlan")
    @ApiOperation("匹配计划")
    @OperationLogAnnotation(operModul = "生活广场-陪伴匹配",operType = "匹配计划",operDesc = "添加")
    public Long matchPlan() {
        return planService.matchPlan();
    }

    /**
     * 检查是否有敏感词
     * @param content
     * @return
     */
    private Boolean check(String content) {
        if (accountService.isInBlackList(ExamineType.PLAN.getCode()))
            throw new BusinessException(ErrorCode.BANED);
        TextReponse textReponse = TextCensor.TextCensor(content);
        if (textReponse.getConclusion().equals("合规"))
            return true;
        else {
            examineService.addExamineByAuto(ExamineType.PLAN.getCode(), null, content);
            throw new ContextException(textReponse.getData().get(0).getMsg());
        }
    }
}
